package com.erp.myAdapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.erp.activity.ChaKanTuZhiAvtivity;
import com.erp.bean.ProdyctionClientBean;
import com.erp.R;
import com.erp.module.crm.xXCrm.guiMingXi.GuimingxiInterface;
import com.erp.util.Myutil;
import com.erp.util.Sputil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/23/023.
 * 作者：刘洋
 */
public class GuimingxiAdapter extends BaseAdapter {
    private List<ProdyctionClientBean.guimingxi> mlist;
    private Context mcontext;
    private LayoutInflater minflater;
    private int mwidth;
    private String onum;
    private GuimingxiInterface guimingxii;

    public GuimingxiAdapter(List<ProdyctionClientBean.guimingxi> list, Context context, String onum, GuimingxiInterface guimingxii) {
        super();
        this.mlist = list;
        this.mcontext = context;
        this.minflater = LayoutInflater.from(context);
        this.mwidth = Sputil.sp("erp").getInt("width", 0);
        this.onum=onum;
        this.guimingxii = guimingxii;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public ProdyctionClientBean.guimingxi getItem(int i) {
        return mlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder vh;
        if (view == null) {
            vh = new ViewHolder();
            view = minflater.inflate(R.layout.lv_guimingxi_item, null);
            vh.tv_guihao = (TextView) view.findViewById(R.id.tv_guihao);
            vh.tv_zhuangtai = (TextView) view.findViewById(R.id.tv_zhuangtai);
            vh.tv_chaidanyuan = (TextView) view.findViewById(R.id.tv_chaidanyuan);
            vh.tv_chaidan_time = (TextView) view.findViewById(R.id.tv_chaidan_time);
            vh.tv_rucang_time = (TextView) view.findViewById(R.id.tv_rucang_time);
            vh.tv_fahuo_time = (TextView) view.findViewById(R.id.tv_fahuo_time);
            vh.tv_name= (TextView) view.findViewById(R.id.tv_name);
            vh.iv = (ImageView) view.findViewById(R.id.iv_gui);
            vh.tv_cte_time = (TextView) view.findViewById(R.id.tv_cte_time);
            vh.tv_cte_problem = (TextView) view.findViewById(R.id.tv_cte_problem);
            vh.ll_gui = (LinearLayout) view.findViewById(R.id.ll_gui);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        ProdyctionClientBean.guimingxi guimingxi = new ProdyctionClientBean.guimingxi();
        guimingxi = mlist.get(i);
        settv(guimingxi.guihao, vh.tv_guihao);
        settv(guimingxi.zhuangtai, vh.tv_zhuangtai);
        settv(guimingxi.chaidanyuan, vh.tv_chaidanyuan);
        settv(guimingxi.chaidanshijian, vh.tv_chaidan_time);
        settv(guimingxi.rucangshijian, vh.tv_rucang_time);
        settv(guimingxi.fahuoshijian, vh.tv_fahuo_time);
        settv(guimingxi.name,vh.tv_name);
        settv(Myutil.getdata(guimingxi.cte_time),vh.tv_cte_time);
        settv(guimingxi.cte_problem,vh.tv_cte_problem);
        String url="";
        if(guimingxi.tuzhis.size()>0) {
            url = guimingxi.tuzhis.get(0);
        }
        Myutil.showhttpphoto(url, vh.iv,mcontext);
        final ArrayList<String> guituzis = guimingxi.tuzhis;
        final ProdyctionClientBean.guimingxi finalGuimingxi = guimingxi;
        vh.ll_gui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                guimingxii.clickCte(finalGuimingxi);
            }
        });
        vh.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                guimingxii.clickTuzhi(finalGuimingxi);
            }
        });
        return view;
    }

    private void settv(String s, TextView tv) {
        if (s != null) {
            if (s.equals("null")) {
                tv.setText("");
            } else {
                tv.setText(s);
            }
        }
    }

    private class ViewHolder {
        TextView tv_guihao, tv_zhuangtai, tv_chaidanyuan, tv_chaidan_time, tv_rucang_time, tv_fahuo_time,tv_name,tv_cte_time,tv_cte_problem;
        ImageView iv;
        LinearLayout ll_gui;
    }

    public void notifyall(List<ProdyctionClientBean.guimingxi> list) {
        mlist = list;
        notifyDataSetChanged();
    }
}
